<template>
  <div>
    <el-breadcrumb
      separator-class="el-icon-arrow-right"
      style="margin: 0 0 20px 0"
    >
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>校园资讯管理</el-breadcrumb-item>
      <el-breadcrumb-item>
        {{ isAdd ? "新增校园资讯" : "修改校园资讯" }}
      </el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="card_box">
      <div class="main">
        <div class="col_box">
          <div class="col_title">认领人：</div>
          <el-input
            v-model="initData.claimant"
            placeholder="请输入认领人"
          ></el-input>
        </div>
        <div class="col_box">
          <div class="col_title">认领人手机号码：</div>
          <el-input
            v-model="initData.claimantTelephone"
            placeholder="请输入认领人手机号码"
          ></el-input>
        </div>
        <div class="col_box">
          <div class="col_title">认领时间:</div>
          <el-date-picker
            v-model="initData.claimantAt"
            type="datetime"
            placeholder="选择日期时间"
            value-format="timestamp"
          >
          </el-date-picker>
        </div>

        <div class="col_box">
          <div class="col_title"></div>
          <el-button type="primary" @click="clickSave">保存资料</el-button>
          <el-button @click="$router.back()">取消</el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>
<script>
import { getByIdLostFound, getLostFoundEdit } from "@/util/lostFound.js";

export default {
  data() {
    return {
      isAdd: false,

      initData: {
        id: null,
        claimant: "",
        claimantTelephone: "",
        claimantAt: "",
        checked: 0,
      },
    };
  },
  created() {
    // 根据Id判断是新增还是修改
    this.isAdd = this.$route.params.id == 0 ? true : false;
    // 修改 根基id获取数据
    this.getByIdLostFound();
  },
  mounted() {},
  methods: {
    // 根据id获取修改数据
    async getByIdLostFound() {
      // 判断是新增还是修改 修改发请求;
      if (this.isAdd) {
        return;
      }
      let { data: res } = await getByIdLostFound({
        id: this.$route.params.id,
      });
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.initData = res.data;
    },

    // 点击按钮保存
    async clickSave() {
      console.log(this.initData);
      // 判空
      if (this.isAddData()) {
        return;
      }
      this.initData.claimantUserId = window.localStorage.getItem("id");
      this.initData.id = this.$route.params.id;
      this.initData.checked = 2;
      let { data: res } = await getLostFoundEdit(this.initData);
      console.log(res);
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.$message.success(res.msg);
      this.$router.push("/admin/lost_and_found/list");
    },
    //判空
    isAddData() {
      if (this.initData.claimant == "" || this.initData.claimant == null) {
        this.$message.error("请输入认领人");

        return true;
      }
      if (
        this.initData.claimantTelephone == "" ||
        this.initData.claimantTelephone == null
      ) {
        this.$message.error("请输入认领人手机号码");

        return true;
      }
      if (this.initData.claimantAt == "" || this.initData.claimantAt == null) {
        this.$message.error("选择日期时间");
        return true;
      }
      if (true) {
      }
      return false;
    },
  },
};
</script>
<style lang="less" scoped>
.card_box {
  min-height: calc(100vh - 200px);
  .header_box {
    display: flex;
    margin-bottom: 24px;
  }
  .main {
    padding: 42px 0;
    .col_box {
      display: flex;
      align-items: center;
      margin-bottom: 24px;
      .col_title {
        width: 150px;
        text-align: right;
        margin-right: 8px;
      }
      .el-input,
      .el-textarea {
        width: 360px;
      }
      /deep/ .el-upload-dragger {
        width: 340px;
        height: 170px;
      }
      .upload-demo {
        margin-bottom: 0.8rem;
      }
    }
  }
}
</style>
